package ai.moran.test.listener.returnListener;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author haitao
 */
public class Producter {

    public static void main(String[] args) throws Exception {
        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("10.80.20.209");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("abm-order");
        connectionFactory.setUsername("dc_shop");
        connectionFactory.setPassword("JF8tga3#s3");


        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        String exchange ="test_return_exchange";
        String routingKey ="test.return";
        String errorRoutingKey="test.error";

        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("replyCode:"+replyCode);
                System.out.println("replyText:"+replyText);
                System.out.println("exchange:"+exchange);
                System.out.println("routingKey:"+routingKey);
                System.out.println("properties:"+ properties);
                System.out.println("body:"+ new String(body));
            }
        });

        String msg = "Hello RabbitMQ!";
        // mandatory 消息无法路由到队列是否自动删除，默认为false自动删除，true不删除进行后续处理
        channel.basicPublish(exchange, routingKey,true, null, msg.getBytes());
        channel.basicPublish(exchange, errorRoutingKey,true, null, msg.getBytes());
        //5 记得要关闭相关的连接
//        channel.close();
//        connection.close();
    }
}
